home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / elvis172.zip / prsvunix.c < prev    next >
C/C++ Source or Header  |  1993-01-06  |  3KB  |  110 lines

  1. /* prsvunix.c */
  2.  
  3. /* This file contains the UNIX-specific parts of the "elvprsv" program. */
  4.  
  5. #if OSK
  6. #define ELVPRSV
  7. #include "osk.c"
  8. #else
  9. #include <sys/stat.h>
  10. #include <pwd.h>
  11. #endif
  12. #ifndef __STDC__
  13. /* some older systems don't declare this in pwd.h, I guess. */
  14. extern struct passwd *getpwuid();
  15. #endif
  16.  
  17. /* This variable is used to add extra error messages for mail sent to root */
  18. char *ps;
  19.  
  20. /* This function returns the login name of the owner of a file */
  21. char *ownername(filename)
  22.     char    *filename;    /* name of a file */
  23. {
  24.     struct stat    st;
  25.     struct passwd    *pw;
  26.  
  27.     /* stat the file, to get its uid */
  28.     if (stat(filename, &st) < 0)
  29.     {
  30.         ps = "stat() failed";
  31.         return "root";
  32.     }
  33.  
  34.     /* get the /etc/passwd entry for that user */
  35.     pw = getpwuid(st.st_uid);
  36.     if (!pw)
  37.     {
  38.         ps = "uid not found in password file";
  39.         return "root";
  40.     }
  41.  
  42.     /* return the user's name */
  43.     return pw->pw_name;
  44. }
  45.  
  46.  
  47. /* This function sends a mail message to a given user, saying that a file
  48.  * has been preserved.
  49.  */
  50. void mail(user, file, when)
  51.     char    *user;    /* name of user who should receive the mail */
  52.     char    *file;    /* name of original text file that was preserved */
  53.     char    *when;    /* description of why the file was preserved */
  54. {
  55.     char    cmd[80];/* buffer used for constructing a "mail" command */
  56.     FILE    *m, *popen();    /* stream used for giving text to the "mail" program */
  57.     char    *base;    /* basename of the file */
  58.  
  59.     /* separate the directory name from the basename. */
  60.     for (base = file + strlen(file); --base > file && *base != SLASH; )
  61.     {
  62.     }
  63.     if (*base == SLASH)
  64.     {
  65.         *base++ = '\0';
  66.     }
  67.  
  68.     /* for anonymous buffers, pretend the name was "foo" */
  69.     if (!strcmp(base, "*"))
  70.     {
  71.         base = "foo";
  72.     }
  73.  
  74.     /* open a pipe to the "mail" program */
  75. #if OSK
  76.     sprintf(cmd, "mail \"-s=%s preserved!\" %s", base, user);
  77. #else /* ANY_UNIX */
  78.     sprintf(cmd, "mail %s >/dev/null 2>/dev/null", user);
  79. #endif
  80.     m = popen(cmd, "w");
  81.     if (!m)
  82.     {
  83.         /* Can't send mail!  Hope the user figures it out. */
  84.         return;
  85.     }
  86.  
  87.     /* Tell the user that the file was preserved */
  88.     fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
  89.     fprintf(m, "was preserved when %s.\n", when);
  90.     fprintf(m, "To recover this file, do the following:\n");
  91.     fprintf(m, "\n");
  92. #if OSK
  93.     fprintf(m, "     chd %s\n", file);
  94. #else /* ANY_UNIX */
  95.     fprintf(m, "     cd %s\n", file);
  96. #endif
  97.     fprintf(m, "     elvrec %s\n", base);
  98.     fprintf(m, "\n");
  99.     fprintf(m, "With fond wishes for a speedy recovery,\n");
  100.     fprintf(m, "                                    Elvis\n");
  101.     if (ps)
  102.     {
  103.         fprintf(m, "\nP.S. %s\n", ps);
  104.         ps = (char *)0;
  105.     }
  106.  
  107.     /* close the stream */
  108.     pclose(m);
  109. }
  110.